<?php
class stYoutubeToolboxuser_model extends stmodel_2_1{

    public function __construct($config) {
        parent::__construct ( $config );
        $this->setTableName($this->prefix.'st_youtube_user');
        $this->setPrimaryKey('id');
    }    
        
    public function saveYoutubeUser($data) {
        $can_save = $this->getuserId($data['user_name']);
        $data['id'] = !empty($data['id'])? $data['id']:NULL;
        if(empty($can_save) || $data['id'] != NULL){
            
            if($this->saveRecords($data)){
                if($data['id']){
                    $message = 'Add new user successful';
                }else{
                    $message = 'Update user successful';
                }
                return $arg = array(
                            'status' => 'success',
                            'message' => __( $message, $config['plugin_name'])
                        );
            }else{
                if($data['id']){
                    $message = 'Add new user unsuccessful';
                }else{
                    $message = 'Update user unsuccessful';
                }
                return $arg = array(
                            'status' => 'error',
                            'message' => __( $message, $config['plugin_name'])
                        );
            }
        }else{
            return $arg = array(
                        'status' => 'error',
                        'message' => __('This user name is already exits', $config['plugin_name'])
                    );
        }

    }

    public function getuserId($name){
        $query = "SELECT id
        FROM $this->_tableName
        WHERE user_name like('$name')
        ";
        $result = $this->get_var($query);
        return $result;
    }

    public function getUserList(){
        $args = array();
        
        $user_id = get_current_user_id();
        $args['where'] = " user_id = $user_id ";
        return $results = $this->getRecords($args);
    }

    public function getUserDetail($id){
        $query = "SELECT *
        FROM $this->_tableName
        WHERE id = $id
        ";
        return $results = $this->get_results($query);
    }
}